import { Message } from 'element-ui';
let obj = {};
/**
 * putLocalStorage 把数据放到本地存储
 * @param {*} key
 * @param {*} value
 */
obj.getBlob = function(url) {
    return new Promise(resolve => {
        const xhr = new XMLHttpRequest();

        xhr.open("GET", url, true);
        xhr.responseType = "blob";
        xhr.onload = () => {
            if (xhr.status === 200) {
                resolve(xhr.response);
            }
        };

        xhr.send();
    });
};
obj.saveAs = function(blob, filename) {
    if (window.navigator.msSaveOrOpenBlob) {
        navigator.msSaveBlob(blob, filename);
    } else {
        const link = document.createElement("a");
        const body = document.querySelector("body");

        link.href = window.URL.createObjectURL(blob);
        link.download = filename;

        // fix Firefox
        link.style.display = "none";
        body.appendChild(link);

        link.click();
        body.removeChild(link);

        window.URL.revokeObjectURL(link.href);
    }
};
obj.download = function(url, filename) {
    let that = this;
    if (url === '') {
        Message.error('找不到指定文件');
        return;
    }
    obj.getBlob(url).then(blob => {
        obj.saveAs(blob, filename);
    });
};


export default obj;